/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aaron.platform.view;

import com.aaron.platform.dao.PageInfo;
import com.aaron.platform.entity.GczxOrderInfo;
import com.aaron.platform.entity.ItscDomainInfo;
import com.aaron.platform.exceptions.PayInfoExistException;
import com.aaron.platform.service.IDomainService;
import com.aaron.platform.service.IOrderMgr;
import com.aaron.platform.service.IPaymentMgr;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import javax.faces.context.FacesContext;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.myfaces.renderkit.html.util.AddResource;
import org.apache.myfaces.renderkit.html.util.AddResourceFactory;

/**
 *
 * @author mac
 */
public class OrderListBean extends DataListBean {

    private static Logger logger = Logger.getLogger(OrderListBean.class);

    private IOrderMgr orderMgr;

    private IPaymentMgr paymentMgr;

    private IDomainService domainMgr;

    private String projectCode;

    private String orderCode;

    private int orderPro;

    private String contactCode;

    private String taskName;

    private int orderStatus;

    private List<SelectItem> orderProList;

    private List<SelectItem> orderStatusList;

    private List<SelectItem> orderTypeList;

    private List<SelectItem> domainList;

    /** 选中查看明细的订单信息 */
    private GczxOrderInfo selectedOrderInfo;

    /** 当前操作 0:view 1:modify */
    private int currentOpertion = 0;


    public void setOrderMgr(IOrderMgr orderMgr) {
        this.orderMgr = orderMgr;
    }

    public void setPaymentMgr(IPaymentMgr paymentMgr) {
        this.paymentMgr = paymentMgr;
    }

    public void setDomainMgr(IDomainService domainMgr) {
        this.domainMgr = domainMgr;
    }

    public OrderListBean() {
        super();
        this.tableTitle = FacesUtil.getString("table_title_orderlist");

         // 删除、编辑、查看操作
        this.deletable = false;
        this.editable = true;
        this.viewable = true;
        multiSelectable = false;

        orderPro = 0;
        orderStatus = 0;
        pagable = true;
        pageInfo = new PageInfo<GczxOrderInfo>();
    }

    @Override
    public void queryData() {

        // 设置查询条件
        Properties properties = new Properties();
        if (!StringUtils.isEmpty(this.projectCode)) {
            properties.setProperty(GczxOrderInfo.PROJECT_CODE, StringUtils.trim(this.projectCode));
        }
        if (!StringUtils.isEmpty(this.orderCode)) {
            properties.setProperty(GczxOrderInfo.ORDER_CODE, StringUtils.trim(this.orderCode));
        }
        if (!StringUtils.isEmpty(this.contactCode)) {
            properties.setProperty(GczxOrderInfo.CONTACT_CODE, StringUtils.trim(this.contactCode));
        }
        if (!StringUtils.isEmpty(this.taskName)) {
            properties.setProperty(GczxOrderInfo.TASK_NAME, StringUtils.trim(this.taskName));
        }
        if (this.orderPro != 0) {
            properties.setProperty(GczxOrderInfo.ORDER_PRO, String.valueOf(this.orderPro));
        }
        if (this.orderStatus != 0) {
            properties.setProperty(GczxOrderInfo.ORDER_STATUS, String.valueOf(this.orderStatus));
        }
        pageInfo = orderMgr.findByCondition(properties, pageInfo.getStartIndex(), pageInfo.getPageSize());
        logger.info("Query order list finished.");
        data.setWrappedData(pageInfo.getItems());
    }

    @Override
    public void initColumnHeaders() {
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "project_code",
                FacesUtil.getString("table_column_projectcode"), false, "6%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "order_code",
                FacesUtil.getString("table_column_ordercode"), false, "9%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "contact_code",
                FacesUtil.getString("table_column_contactcode"), false, "21%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "enterprise_name",
                FacesUtil.getString("table_column_entprisename"), false, "18%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "task_name",
                FacesUtil.getString("table_column_taskname"), false, "18%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "order_value",
                FacesUtil.getString("table_column_ordervalue"), false, "8%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "order_status",
                FacesUtil.getString("table_column_orderstatus"), false, "6%"));
    }

    @Override
    public String getColumnValue() {
        String columnName = getColumnName();
        increaseColumnIndex();
        
        GczxOrderInfo obj = (GczxOrderInfo) data.getRowData();
        if (columnName.equals("project_code")) {
             return obj.getProjectCode();
        } else if (columnName.equals("order_code")) {
            return obj.getOrderCode();
        } else if (columnName.equals("contact_code")) {
            return obj.getContactCode();
        } else if (columnName.equals("enterprise_name")) {
            return obj.getEnterpriseInfo().getEnterpriseName();
        } else if (columnName.equals("task_name")) {
            return obj.getTaskName();
        } else if (columnName.equals("order_value")) {
            return String.valueOf(obj.getOrderValue());
        } else if (columnName.equals("order_status")) {
            return obj.getDisplayOrderStatus();
        }
        else {
            return null;
        }

    }

    public void selectOrder() {
        List<GczxOrderInfo> selectedOrderList = new ArrayList<GczxOrderInfo>();
        List<GczxOrderInfo> orderInfos = (List<GczxOrderInfo>) this.data.getWrappedData();
        for (GczxOrderInfo orderInfo : orderInfos) {
            if (orderInfo.isSelected()) {
                selectedOrderList.add(orderInfo);
            }
        }
        PaymentInfoBean paymentInfoBean = (PaymentInfoBean) FacesUtil.getManagedBean("paymentBean");
        paymentInfoBean.addData(selectedOrderList);

        //清空查询的值
        reset();

        //关闭弹出窗口，刷新主窗口
        FacesContext facesContext = FacesContext.getCurrentInstance();
        String javaScriptText =
                "window.opener.location.href='input_payment_wave.jsf'; window.close();";
        // Add the Javascript to the rendered page's header for immediate execution
        AddResource addResource = AddResourceFactory.getInstance(facesContext);
        addResource.addInlineScriptAtPosition(facesContext,
                AddResource.HEADER_BEGIN, javaScriptText);
    }

    /**
     * 初始化查询条件
     */
    public void reset()
    {
        this.projectCode = "";
        this.orderCode = "";
        this.contactCode = "";
        this.orderPro = 0;
        this.taskName = "";
        this.orderStatus = 0;
        this.data = new ListDataModel();
        pageInfo = new PageInfo<GczxOrderInfo>();
    }

    /** 获取专业列表 */
    public List<SelectItem> getOrderProList() {
        if (this.orderProList == null) {
            orderProList = new ArrayList<SelectItem>();
            orderProList.add(new SelectItem(0, FacesUtil.getString("list_item_default")));
            orderProList.add(new SelectItem(1, FacesUtil.getString("list_item_jz")));
            orderProList.add(new SelectItem(2, FacesUtil.getString("list_item_cs")));
            orderProList.add(new SelectItem(3, FacesUtil.getString("list_item_fgys")));
            orderProList.add(new SelectItem(4, FacesUtil.getString("list_item_qyw")));
        }
        return orderProList;
    }

    public List<SelectItem> getOrderStatusList() {
         if (this.orderStatusList == null) {
            orderStatusList = new ArrayList<SelectItem>();
            orderStatusList.add(new SelectItem(0, FacesUtil.getString("list_item_default")));
            orderStatusList.add(new SelectItem(1, FacesUtil.getString("list_item_executing")));
            orderStatusList.add(new SelectItem(2, FacesUtil.getString("list_item_finished")));
        }
        return orderStatusList;
    }

    public List<SelectItem> getDomainList() {
         if (this.domainList == null) {
            domainList = new ArrayList<SelectItem>();
            List<ItscDomainInfo> domainInfos = domainMgr.findAllDomains();
            for (ItscDomainInfo domainInfo : domainInfos) {
                this.domainList.add(new SelectItem(domainInfo.getDomainId(), domainInfo.getDomainName()));
            }
        }
        return this.domainList;
    }

    /**
     * 查看明细
     * @return 返回页面导航
     */
    @Override
    public String showDetail()
    {
        // 设置操作类型：view
        this.currentOpertion = 0;

        GczxOrderInfo orderInfo = (GczxOrderInfo) data.getRowData();
        this.selectedOrderInfo = orderInfo;
        return "order_detail";
    }

    /**
     * 编辑数据：修改审计信息
     * @return 返回页面导航
     */
    @Override
    public String editData()
    {
        // 设置操作类型：modify
        this.currentOpertion = 1;

        GczxOrderInfo orderInfo = (GczxOrderInfo) data.getRowData();
        this.selectedOrderInfo = orderInfo;
        return "order_detail";
    }
    
    @Override
    public String deleteData() {
        
        // 设置操作类型：delete
        this.currentOpertion = 2;
        
        GczxOrderInfo orderInfo = (GczxOrderInfo) data.getRowData();
        try {
            orderMgr.removeOrderInfo(orderInfo.getOrderId());
        } catch (PayInfoExistException ex) {
            FacesUtil.addErrorMessage(null, ex.getErrorCode(), orderInfo.getOrderCode());
        }
        return null;
    }

    public String saveOrderDetail() {
        orderMgr.updateOrderInfo(selectedOrderInfo);
        FacesUtil.addInfoMessage(null, "info_update_order_success" , selectedOrderInfo.getOrderCode());

        // 更新数据
        refreshData();
        return "order_search";
    }

    public String invoiceDetail() {
        return null;
    }

    public String toLastPage() {
        return "order_search";
    }

    public String getContactCode() {
        return contactCode;
    }

    public void setContactCode(String contactCode) {
        this.contactCode = contactCode;
    }

    public static Logger getLogger() {
        return logger;
    }

    public static void setLogger(Logger logger) {
        OrderListBean.logger = logger;
    }

    public String getOrderCode() {
        return orderCode;
    }

    public void setOrderCode(String orderCode) {
        this.orderCode = orderCode;
    }

    public int getOrderPro() {
        return orderPro;
    }

    public void setOrderPro(int orderPro) {
        this.orderPro = orderPro;
    }

    public int getOrderStatus() {
        return orderStatus;
    }

    public void setOrderStatus(int orderStatus) {
        this.orderStatus = orderStatus;
    }

    public String getProjectCode() {
        return projectCode;
    }

    public void setProjectCode(String projectCode) {
        this.projectCode = projectCode;
    }

    public String getTaskName() {
        return taskName;
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    public GczxOrderInfo getSelectedOrderInfo() {
        return selectedOrderInfo;
    }

    public int getCurrentOpertion() {
        return currentOpertion;
    }
}
